<template>
  <div>
    <el-form
    style="width: 350px;"
    ref="form"
    :rules="rules"
    :model="form"
    label-width="100px">
      <el-form-item :label="name" prop="actorName">
        <el-input v-model="form.actorName"></el-input>
      </el-form-item>
      <el-form-item :label="avatar" prop="actorAvatar">
        <el-upload
          class="avatar-uploader"
          :action="`${UPLOADURL}/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.actorAvatar" :src="form.actorAvatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">立即新增</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from '@/http'

  export default {
    props:['name','avatar'],
    data() {
      return {
        form: {
          actorName: '',
          actorAvatar: ''
        },
        rules: {
          actorName:{required:true,message:'该字段不能为空',trigger:'blur'},
          actorAvatar: {required:true,message:'该字段不能为空',trigger:'blur'}
        }
      }
    },

    methods: {
      submit() {
        this.$refs.form.validate(valid=>{
          if(valid){
            console.log('验证通过')

            if(this.$route.path === '/home/actor-add'){
              // 发送请求
              httpApi.actorApi.add(this.form).then(res=>{
                console.log('新增结果',res)
                if(res.data.code == 200){
                  this.$message({
                    type:'success',
                    message:'新增成功'
                  })
                  // 重置表单
                  this.$refs.form.resetFields()
                }else{
                  this.$message.error('新增失败,请稍后重试')
                }
              })
            }

            if(this.$route.path === '/home/director-add'){
              // 发送请求
              httpApi.directorApi.add(this.form).then(res=>{
                console.log('新增结果',res)
                if(res.data.code == 200){
                  this.$message({
                    type:'success',
                    message:'新增成功'
                  })
                  // 重置表单
                  this.$refs.form.resetFields()
                }else{
                  this.$message.error('新增失败,请稍后重试')
                }
              })
            }
            

          }else{
            console.log('验证失败')
          }
        })
      },
      reset(){
        this.$refs.form.resetFields()
      },
      handleAvatarSuccess(res, file) {
        console.log(res)
        this.form.actorAvatar = res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    },
  }
</script>

<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
  }
  .avatar {
    width: 128px;
    display: block;
  }
</style>